home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_08_03
/
8n03049a
< prev
next >
Wrap
Text File
|
1990-03-18
|
2KB
|
75 lines
*****Listing 2*****
// Node[T] - Implementation
// Parametrize the file by T ...
typedef int T;
typedef int Truth;
class Node {
public:
Node(T x) { Next = 0; val = x; }
Node *next() { return Next; }
T value() { return val; }
void link( Node *p) { Next = p; }
private:
Node *Next;
T val;
};
class Sloop {
public:
Sloop() { before = after = 0; }
Truth isbegin() { return (before == 0); }
Truth isend() { return (after == 0); }
void next() { move( &after, &before); }
void prev() { move( &before, &after); }
T geta() { return after->value(); }
T getb() { return before->value(); }
Truth puta( T x) { return put( x, &after); }
Truth putb( T x) { return put( x, &before); }
T dela() { return del( &after); }
T delb() { return del( &before); }
private:
Node *before,
*after;
// these private functions are candidates for static
// in C++ 2.0
// move moves a node from the head of the
// pre list to the head of the post list.
// pre is before point, post is after point
void move( Node **pre, Node **post)
{
if( (*pre) ) {
Node *t = (*pre);
(*pre) = (*pre)->next();
t->link((*post));
(*post) = t;
}
}
Truth put( T x, Node **p)
{
Node *t;
if( t = new Node(x)) {
t->link( *p);
*p = t;
}
return (t != 0 );
}
T del( Node **p)
{
Node *t = (*p);
(*p) = (*p)->next();
T r;
r = t->value();
delete t;
return r;
}
};